package flychess.data;

import flychess.util.XMLParser;
import java.util.HashSet;
import java.util.Random;

public class RandomGameMap extends GameMap{
    private final HashSet<Integer> generatePosSet = new HashSet<>();
    private final Random rand = new Random();
    public RandomGameMap(int  len) {
        this.mapLen = len;
        this.map = new int[this.mapLen];
        this.luckyTurn = new int[XMLParser.getPosNum("luckyTurnNum")];
        this.landMine = new int[XMLParser.getPosNum("landMineNum")];
        this.pause = new int[XMLParser.getPosNum("pauseNum")];
        this.timeTunnel = new int[XMLParser.getPosNum("timeTunnelNum")];
        // 随机生成一些坐标
        generatePos(luckyTurn);
        generatePos(landMine);
        generatePos(pause);
        generatePos(timeTunnel);
    }

    private void generatePos(int[] arr) {
        int pos;
        for (int i = 0; i < arr.length; i++) {
            while (true) {
                pos = rand.nextInt(this.mapLen - 2) + 1;
                // 生成的位置不能在地图的起始点，并且不能重复
                if (!generatePosSet.contains(pos)) {
                    generatePosSet.add(pos);
                    break;
                }
            }
            arr[i] = pos;
        }
    }
}
